package main

import (
	"fmt"
	"os"
	"strconv"
)

func processPBCArgs(args []string, port *string) {
	// no valid args
	if len(args) < 2 {
		usagePbc()
	}

	// proceed by args
	switch args[1] {
	case "-h":
		usagePbc()
	case "-v":
		versionPbc()
	case "-p":
		getPort(args, port)
	default:
		usagePbc()
	}
}

func usagePbc() {
	fmt.Printf(`PBC %s - a http(s) proxy that easy to config and start

Usage: you can start pbc at port 7799 with command ./pbc -p 7799 &

Args:
	-p	Specify PBC client port to start
	-h	Help and quit
	-v	Show current version and quit

`, pbcVERSION)
	os.Exit(0)
}

func versionPbc() {
	fmt.Printf(`PBC - a http(s) proxy that easy config and start
Version: %s
`, pbcVERSION)
	os.Exit(0)
}

func getPort(args []string, port *string) {
	portAlert := "Invalid port, please input a valid port (1024 ~ 65535)."
	if len(args) < 3 {
		fmt.Println(portAlert)
		os.Exit(0)
	}

	// check port input
	intPort, err := strconv.Atoi(args[2])
	if err != nil || intPort < 1024 || intPort > 65535 {
		fmt.Println(portAlert)
		os.Exit(0)
	}

	*port = args[2]
}
